﻿using System;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LKCMS.Common;
using LKCMS.Web.UI;

namespace LKCMS.Web.Admin.Contents
{
    public partial class List : LKCMS.Web.UI.ManagePage
    {
        public int pcount;                      //总条数
        public int page;                        //当前页
        public int pagesize;                    //设置每页显示的大小
        public readonly int kindId = (int)Channel.Contents;  //类别种类

        public int classId;
        public string keywords = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.pagesize = webset.ContentPageNum;
            if (!int.TryParse(Request.Params["page"] as string, out this.page))
            {
                this.page = 0;
            }
            if (!int.TryParse(Request.Params["classId"] as string, out this.classId))
            {
                this.classId = 0;
            }
            if (!string.IsNullOrEmpty(Request.Params["keywords"]))
            {
                this.keywords = Request.Params["keywords"].Trim();
            }

            if (!Page.IsPostBack)
            {
                chkLoginLevel("viewContents");
                //绑定类别
                ChannelTreeBind(0, "所有类别", (int)Channel.Contents, this.ddlClassId);
                this.RptBind("Id>0" + CombSqlTxt(this.kindId, this.classId, this.keywords));
            }
        }

        #region 数据列表绑定
        private void RptBind(string strWhere)
        {
            LKCMS.BLL.Contents bll = new LKCMS.BLL.Contents();
            //获得总条数
            this.pcount = bll.GetCount(strWhere);
            if (this.pcount > 0)
            {
                this.lbtnDel.Enabled = true;
            }
            else
            {
                this.lbtnDel.Enabled = false;
            }
            if (this.classId > 0)
            {
                this.ddlClassId.SelectedValue = this.classId.ToString();
            }
            if (!string.IsNullOrEmpty(this.keywords))
            {
                this.txtKeywords.Text = this.keywords;
            }

            this.rptList.DataSource = bll.GetPageList(this.pagesize, this.page, strWhere, "SortId asc,Id desc");
            this.rptList.DataBind();
        }
        #endregion

        //查询
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            int _classId;
            string _keywords = "";
            if (!int.TryParse(this.ddlClassId.SelectedValue, out _classId))
            {
                _classId = 0;
            }
            if (!string.IsNullOrEmpty(this.txtKeywords.Text))
            {
                _keywords = this.txtKeywords.Text;
            }
            //转向页面
            Response.Redirect("List.aspx?" + CombUrlTxt(_classId, _keywords) + "page=0");
        }

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delContents");
            LKCMS.BLL.Contents bll = new LKCMS.BLL.Contents();
            //批量删除
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                if (cb.Checked)
                {
                    //保存日志
                    SaveLogs("[内容模块]删除内容页：" + bll.GetModel(id).Title);
                    bll.Delete(id);
                }
            }
            JscriptPrint("批量删除成功啦！", "List.aspx?" + CombUrlTxt(this.classId, this.keywords) + "page=0", "Success");
        }
    }
}
